package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.MessageDao;
import entity.Message;

/**
 * Servlet implementation class MessageServlet
 */
@WebServlet("/MessageServlet")
public class MessageServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MessageServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        List<Message> mlist = new MessageDao().query();
        HttpSession session = request.getSession();
        int numberOfRecords = mlist.size();//记录总数
        int recordsPerPage = 5;//每页显示的记录数
        int totalPages = numberOfRecords%recordsPerPage==0?//总页数
                numberOfRecords/recordsPerPage:
                numberOfRecords/recordsPerPage+1;

        session.setAttribute("mlist", mlist);
        session.setAttribute("numberOfRecords", mlist.size());
        session.setAttribute("recordsPerPage", recordsPerPage);
        session.setAttribute("currentPage", totalPages);//默认显示最后一页
        response.sendRedirect("message.jsp");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
